package com.aaa.tars.controller;

import com.aaa.tars.entity.Visitors;
import com.aaa.tars.service.VisitorsService;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.UUID;

/**
 * @FileName: VisitorsController
 * @Description: 游客控制器
 * @CreateTime: 2025/9/12 16:52
 * @Version: 17
 */
@RestController
@RequestMapping("visitors")
public class VisitorsController {

    @Resource
    private  VisitorsService visitorsService;
    private String uploadDir;

    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Visitors> page) {
        return ResultUtil.success(visitorsService.queryByPage(page));
    }

    @GetMapping("queryById")
    public Result queryById(@RequestParam Integer visitorId) {
        return ResultUtil.success(visitorsService.queryById(visitorId));
    }

    @PostMapping("add")
    public Result add(@RequestBody Visitors visitors) {
        return ResultUtil.success(visitorsService.add(visitors));
    }

    @PutMapping("update")
    public Result update(@RequestBody Visitors visitors) {
        return ResultUtil.success(visitorsService.update(visitors));
    }

    @DeleteMapping("deleteById")
    public Result deleteById(@RequestParam(required = false) Integer visitorId) {
        if (visitorId == null) {
            return ResultUtil.error("游客ID不能为空");
        }
        return ResultUtil.success(visitorsService.deleteById(visitorId));
    }
    /**
     * 上传头像
     * @param file 上传的文件
     * @param visitorId 游客ID
     * @return 上传结果和文件路径
     */
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file,
                               @RequestParam(required = false) Integer visitorId) {
        if (visitorId == null) {
            return ResultUtil.error("游客ID不能为空");
        }
        return visitorsService.uploadAvatar(file, visitorId);
    }
    /**
     * 根据手机号查找游客
     */
    @GetMapping("findByPhoneNumber")
    public Result findByPhoneNumber(@RequestParam String phoneNumber) {
        Visitors visitor = visitorsService.findByPhoneNumber(phoneNumber);
        return ResultUtil.success(visitor != null);
    }

    /**
     * 重置密码
     */
    @PostMapping("resetPassword")
    public Result resetPassword(@RequestBody Map<String, String> params) {
        String phoneNumber = params.get("phoneNumber");
        String newPassword = params.get("newPassword");

        if (phoneNumber == null || newPassword == null) {
            return ResultUtil.error("参数不能为空");
        }

        boolean result = visitorsService.resetPassword(phoneNumber, newPassword);
        return result ? ResultUtil.success("密码重置成功") : ResultUtil.error("密码重置失败");
    }
}